<?php
/*
Plugin Name: Region IP Wid
Plugin URI: http://vmrf.ru
Description: Show your current region
Author: vmrf.ru
Version: 1.0
Author URI: http://vmrf.ru
 */

namespace AY\Widget;

class Regip extends \AY\Widget\AWidget
{
    protected $widgetName = 'Regip';
    protected $widgetOptions = array(
        'classname' => 'regip-ay',
        'description'   => 'Region ip widget'
    );

    function widget( $args, $instance ) {
        if ( isset($_REQUEST['region'])) {
		
                ay()->region()->setRegion(intval($_REQUEST['region']));
        }
        ?>
        <script type="text/javascript">
            $(function() {
                $(document).ready(function() {
                    jQuery.post('<?php echo ay()->config()->AJAXHost; ?>', {action: 'Regip'}, function(data){
                        data=jQuery.parseJSON(data);
                        tmpl = "<div style=\"background: url('<?php echo ay()->config()->get_template_directory_uri(); ?>/assets/images/regions/"+data.code+".png') no-repeat scroll left top transparent; height: 30px; padding: 18px 0 4px 70px; \"><span><a id=\"popup_link\" href='javascript:SelectRegionDialog.show();'>"+data.name+"</a></span></div>";
                        $('#reg').html(tmpl);
                    });
                    return false;
                });

                $('#sendRegion').click(function() {
                    jQuery.post('<?php echo ay()->config()->AJAXHost; ?>',
                                {action: 'Regip', region: $("#region :selected").val()},
                                function(data){
                                    data=jQuery.parseJSON(data);
                                    //$('#reg').html(data.name);
									tmpl = "<div style=\"background: url('<?php echo ay()->config()->get_template_directory_uri(); ?>/assets/images/regions/"+data.code+".png') no-repeat scroll left top transparent; height: 30px; padding: 18px 0 4px 70px; \"> <span><a id=\"popup_link\" href='javascript:SelectRegionDialog.show();'>"+data.name+"</a></span></div>";
									$('#reg').html(tmpl);
                                });
                    return false;
                });
				
				setRegion = function(value) {
					jQuery.post('<?php echo ay()->config()->AJAXHost; ?>',
                                {action: 'Regip', region: value},
                                function(data){
                                    data=jQuery.parseJSON(data);
									tmpl = "<div style=\"background: url('<?php echo ay()->config()->get_template_directory_uri(); ?>/assets/images/regions/"+data.code+".png') no-repeat scroll left top transparent; height: 30px; padding: 18px 0 4px 70px; \"> <span><a id=\"popup_link\" href='javascript:SelectRegionDialog.show();'>"+data.name+"</a></span></div>";
									$('#reg').html(tmpl);
                                                                        document.location.href = document.location.href;
                                });
                    return false;
				}
            });
        </script>
        <div id="reg">
        </div>
		<?php
        /*Сменить регион:<br>
        <form method="post">
            <select id="region" name="region">
        <?php
            foreach(ay()->config()->listRegion as $key=>$value){
                echo("<option " . $cur . " value='" . $key . "'>" . $value['name'] . "</option>");
            }
        ?>
            </select>
            <input type='hidden' name='_wpnonce' value="<?php echo wp_create_nonce('regip'); ?>"/>
            <input id='sendRegion' type='submit'/>
        </form>*/
		
    }
    
    function form( $instance ) {
    }
        
    public function ajax() {
        if (isset($_REQUEST['region'])){
             ay()->region()->setRegion(intval($_REQUEST['region']));
        }
        $name = ay()->region()->getRegionName() ? ay()->region()->getRegionName() : "Российская Федерация";
        $code = ay()->region()->getRegionByName() ? ay()->region()->getRegionByName() : 0;
        $param = array('name' => $name, 'code' => $code);
        $_SESSION['region']=$_REQUEST['region'];
        return $param;
    }
}
